package com.ygqh.baby.service.ngmm.api;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.model.ngmm.NgmmOrderInfoResult;
import com.ygqh.baby.model.ngmm.NgmmOrderListResult;
import com.ygqh.baby.model.ngmm.NgmmResult;
import com.ygqh.baby.service.ngmm.exception.NgmmException;
import com.ygqh.baby.service.ngmm.http.NgmmResponse;
import com.ygqh.baby.service.ngmm.util.NgmmConfigure;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.ExceptionUtil;
import com.ygqh.baby.utils.MailUtil;

@Component
public class NgmmApi extends MpApi {
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private MailUtil mailUtil;
	
	/**
	 * OMS订单列表查询
	 * @param startTime		开始时间
	 * @param endTime		结束时间
	 * @param pageNo		页码
	 * @param pageSize		每页数量,建议为40-100
	 * @throws NgmmException 
	 */
	public NgmmOrderListResult orderQueryList(Date startTime, Date endTime, Integer pageNo, Integer pageSize, 
			String orderStatus, String timeType) throws NgmmException {
		Map<String, String> param = getApplicationParam();
		param.put("method", getRequestUri("order.query.list"));
		if (StringUtil.isNotBlank(orderStatus)) {
			param.put("order_status", orderStatus);
		}
		if (StringUtil.isNotBlank(timeType)) {
			param.put("time_type", timeType);
		}
		if (StringUtil.isNotBlank(getRequestUri("warehouse.code"))) {
			param.put("warehouse_code", getRequestUri("warehouse.code"));
		}
		if (startTime != null) {
			param.put("start_time", DateConvertUtils.formatDateTime(startTime));
		}
		if (endTime != null) {
			param.put("end_time", DateConvertUtils.formatDateTime(endTime));
		} else {
			param.put("end_time", DateConvertUtils.formatDateTime(new Date()));
		}
		if (pageNo != null) {
			param.put("page_no", pageNo.toString());
		}
		if (pageSize != null) {
			param.put("page_size", pageSize.toString());
		}
		String app_sign = getSign(param);
		param.put("app_sign", app_sign);
		NgmmResponse response = ngmmRequestExecutor.post(getRequestUri(NgmmConfigure.API_BASE_URL), param);
		try {
			return response.getAsObject(new TypeReference<NgmmOrderListResult>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			try {
				mailUtil.sendWithHtml(
						new String[]{"sunshuo@yiigoo.cn"}, "年糕妈妈订单", "orderQueryInfo:[param="+param.toString()+", response=" + response.getAsString() + ", error=" +ExceptionUtil.getExceptionMsg(e));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * OMS指定单号查询订单
	 * @param orderNo		订单号,多个用逗号分开
	 * @throws NgmmException 
	 */
	public NgmmOrderInfoResult orderQueryInfo(String orderNo) throws NgmmException {
		Map<String, String> param = getApplicationParam();
		if (StringUtil.isBlank(orderNo)) {
			return null;
		}
		param.put("order_no", orderNo);
		param.put("method", getRequestUri("order.query.info"));
		String app_sign = getSign(param);
		param.put("app_sign", app_sign);
		NgmmResponse response = ngmmRequestExecutor.post(getRequestUri(NgmmConfigure.API_BASE_URL), param);
		try {
			return response.getAsObject(new TypeReference<NgmmOrderInfoResult>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			try {
				mailUtil.sendWithHtml(new String[]{"sunshuo@yiigoo.cn"}, "年糕妈妈订单", "orderQueryInfo:[orderNo="+orderNo+", response=" + response.getAsString() + ", error=" +ExceptionUtil.getExceptionMsg(e));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * OMS订单	修改快递
	 * @param orderNo		订单号
	 * @param expressCode	快递方式CODE
	 * @param expressNo		快递单号
	 * @throws NgmmException 
	 */
	public NgmmResult orderUpdateExpress(String orderNo, String expressCode, String expressNo) throws NgmmException {
		Map<String, String> param = getApplicationParam();
		if (StringUtil.isBlank(orderNo)) {
			return null;
		}
		param.put("order_no", orderNo);
		param.put("method", getRequestUri("order.update.express"));
		if (StringUtil.isNotBlank(expressCode)) {
			param.put("express_code", expressCode);
		}
		if (StringUtil.isNotBlank(expressNo)) {
			param.put("express_no", expressNo);
		}
		String app_sign = getSign(param);
		param.put("app_sign", app_sign);
		NgmmResponse response = ngmmRequestExecutor.post(getRequestUri(NgmmConfigure.API_BASE_URL), param);
		return response.getAsObject(new TypeReference<NgmmResult>() {
		});
	}
	
}
